package servlet.controller;

import entity.Book;
import org.apache.commons.beanutils.BeanUtils;
import service.BookService;
import service.impl.BookServiceImpl;
import util.PageHelper;
import util.StrUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * User: key
 * ClassName:BookController
 * Date: 2023/5/5
 * Time: 19:54
 * description:
 */
@WebServlet("/book")
public class BookController extends HttpServlet {
    private BookService bs = new BookServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if(method == null){
            method = "error";
        }
        switch (method){
            case "getAll":
                getAll(req,resp);
                break;
            case "getByPage":
                getByPage(req,resp);
                break;
            case "del":
                del(req,resp);
                break;
            case "getOne":
                getOne(req,resp);
                break;
            case "add":
                try {
                    add(req,resp);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case "update":
                try {
                    update(req,resp);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case "error":
            default:
                req.setAttribute("msg","(●'◡'●)");
                req.getRequestDispatcher("indexMessage.jsp").forward(req,resp);
        }
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String,String[]> parameterMap = req.getParameterMap();
        Book b = new Book();
        try {
            BeanUtils.populate(b,parameterMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        b.setPublishDate(StrUtils.strToDate(req.getParameter("pd")));
        String msg = "新增失败";
        if(bs.insertBook(b)){
            msg = "新增成功";
        }
        req.setAttribute("msg",msg);
        req.getRequestDispatcher("indexMessage.jsp").forward(req,resp);
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws  ServletException, IOException {
        Map<String,String[]> parameterMap = req.getParameterMap();
        Book b = new Book();
        try {
            BeanUtils.populate(b,parameterMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        b.setPublishDate(StrUtils.strToDate(req.getParameter("pd")));
        String msg = "新增失败";
        if(bs.insertBook(b)){
            msg = "新增成功";
        }
        req.setAttribute("msg",msg);
        req.getRequestDispatcher("indexMessage.jsp").forward(req,resp);
    }

    private void getOne(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = StrUtils.strToInteger(req.getParameter("id"));
        Book book = bs.getOneById(id);
        req.setAttribute("b",book);
        req.getRequestDispatcher("updateBook.jsp").forward(req,resp);
    }

    private void del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = StrUtils.strToInteger(req.getParameter("id"));
        String msg = "删除失败";
        if(bs.delBook(id)){
            msg = "删除成功";
        }
        req.setAttribute("msg",msg);
        req.getRequestDispatcher("indexMessage.jsp").forward(req,resp);
    }

    private void getByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNum = StrUtils.strToInteger(req.getParameter("pageNum"));
        PageHelper he = bs.getByPage(pageNum,5);
        req.setAttribute("helper",he);
        req.getRequestDispatcher("index.jsp").forward(req,resp);
    }

    private void getAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Book> blist = bs.getAll();
        req.setAttribute("bookList",blist);
        req.getRequestDispatcher("index.jsp").forward(req,resp);
    }
}
